package com.zpark.drug.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.mapper.ChecklistMapper;
import com.zpark.drug.pojo.Checklist;
import com.zpark.drug.service.ChecklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class ChecklistServiceImpl implements ChecklistService {
    @Autowired
    private ChecklistMapper checklistMapper;

    @Override
    public List<Checklist> findAll() {
        return checklistMapper.selectAll();
    }

    @Override
    public Checklist findById(Integer id) {
        return checklistMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addChecklist(Checklist checklist) {
        return checklistMapper.insert(checklist);
    }

    @Override
    public int updateChecklist(Checklist checklist) {
        return checklistMapper.updateByPrimaryKey(checklist);
    }

    @Override
    public int deleteChecklistById(Integer id) {
        return checklistMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Checklist> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Checklist.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("name", "%" + param + "%") // 使用 orLike 来实现多字段模糊查询
                    .orLike("type", "%" + param + "%")
                    .orLike("operator", "%" + param + "%");
        }
        List<Checklist> checklists;
        if (param != null && !param.isEmpty()) {
            checklists = checklistMapper.selectByExample(example);
        } else {
            checklists = checklistMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(checklists);
    }
}
